package org.travelfusion.xmlclient.ri.handler.plane;

import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;

import org.travelfusion.xmlclient.impl.handler.StAXSubResponseHandlerParent;
import org.travelfusion.xmlclient.ri.xobject.plane.XRouterSummary;
import org.travelfusion.xmlclient.xobject.XRequest;

class RouterSummarySubHandler {

  private final StAXSubResponseHandlerParent<? extends XRequest> parent;

  RouterSummarySubHandler(StAXSubResponseHandlerParent<? extends XRequest> parent) {
    this.parent = parent;
  }

  private static enum State {
    ROOT, //
    VENDOR, //
    LOCATIONS, //
    ORIGIN, //
    DESTINATION
  }

  public XRouterSummary handleResponse() throws Exception {
    XRouterSummary summary = new XRouterSummary();

    StringBuilder builder = new StringBuilder();
    String currentName = null;

    final XMLStreamReader responseReader = parent.getResponseReader();

    State state = State.ROOT;
    done: for (int event; ((event = responseReader.next()) != XMLStreamConstants.END_DOCUMENT);) {
      switch (event) {
        case XMLStreamConstants.START_ELEMENT:
          currentName = responseReader.getName().getLocalPart();
          builder.setLength(0);
          switch (state) {
            case ROOT:
              if ("Vendor".equals(currentName)) {
                state = State.VENDOR;
              }
              else if ("RequestedLocations".equals(currentName)) {
                state = State.LOCATIONS;
              }
              break;
            case LOCATIONS:
              if ("Origin".equals(currentName)) {
                state = State.ORIGIN;
              }
              else if ("Destination".equals(currentName)) {
                state = State.DESTINATION;
              }
              break;
          }
          break;
        case XMLStreamConstants.CHARACTERS:
          builder.append(responseReader.getTextCharacters(), responseReader.getTextStart(), responseReader.getTextLength());
          break;
        case XMLStreamConstants.END_ELEMENT:
          currentName = responseReader.getName().getLocalPart();
          switch (state) {
            case ROOT:
              if ("Supplier".equals(currentName)) {
                summary.setSupplier(builder.toString());
              }
              else if ("Complete".equals(currentName)) {
                summary.setComplete(Boolean.parseBoolean(builder.toString()));
              }
              else if ("GroupList".equals(currentName)) {
                break done;
              }
              else if ("Router".equals(currentName)) {
                break done;
              }
              else if ("RouterList".equals(currentName)) {
                break done;
              }
              break;
            case VENDOR:
              if ("Name".equals(currentName)) {
                summary.setVendor(builder.toString());
              }
              else if ("Url".equals(currentName)) {
                summary.setVendorUrl(builder.toString());
              }
              else if ("Vendor".equals(currentName)) {
                state = State.ROOT;
              }
              break;
            case LOCATIONS:
              if ("RequestedLocations".equals(currentName)) {
                break done;
              }
              break;
            case ORIGIN:
              if ("Code".equals(currentName)) {
                summary.setOrigin(builder.toString());
                state = State.LOCATIONS;
              }
              break;
            case DESTINATION:
              if ("Code".equals(currentName)) {
                summary.setDestination(builder.toString());
                state = State.LOCATIONS;
              }
              break;
          }
          break;
      }
    }

    return summary;
  }
}
